﻿using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace AccelerometerTestApp
{
    public partial class Program 
    {

        GTM.Seeed.Accelerometer acc = new GTM.Seeed.Accelerometer(3);
        void ProgramStarted()
        {
            /******************************************************************************************
            Access modules defined in the designer by typing their name:                            
            
            e.g.  button
                  camera1

            Initialize event handlers here.
            e.g. button.ButtonPressed += new GTM.MSR.Button.ButtonEventHandler(button_ButtonPressed);             
            ***************************************************************************************** */

            acc.LoadCalibration();

            acc.MeasurementComplete += new GTM.Seeed.Accelerometer.MeasurementCompleteEventHandler(acc_MeasurementComplete);
            acc.ThresholdExceeded += new GTM.Seeed.Accelerometer.ThresholdExceededEventHandler(acc_ThresholdExceeded);
            acc.MeasurementRange = GTM.Seeed.Accelerometer.Range.TwoG;

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            button1.ButtonPressed += new Button.ButtonEventHandler(button1_ButtonPressed);
            button2.ButtonPressed += new Button.ButtonEventHandler(button2_ButtonPressed);
            button3.ButtonPressed += new Button.ButtonEventHandler(button3_ButtonPressed);

            // Do one-time tasks here
            Debug.Print("Program Started");     
        }
        
        
        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            // Test calibration
            acc.Calibrate();
            acc.SaveCalibration();
            acc.LoadCalibration();
        }

        void button1_ButtonPressed(Button sender, Button.ButtonState state)
        {
            acc.EnableThresholdDetection(-0.9, false, false, true, true, false, false);
        }

        void button2_ButtonPressed(Button sender, Button.ButtonState state)
        {
            acc.ResetThresholdDetection();
        }

        void button3_ButtonPressed(Button sender, Button.ButtonState state)
        {
            acc.ContinuousMeasurementInterval = new TimeSpan(0,0,0,0,300); // 300 ms
            acc.StartContinuousMeasurements();
        }

        void acc_MeasurementComplete(GTM.Seeed.Accelerometer sender, GTM.Seeed.Accelerometer.Acceleration acceleration)
        {
            double mag = acceleration.X * acceleration.X + acceleration.Y * acceleration.Y + acceleration.Z * acceleration.Z;
            Debug.Print(acceleration.ToString() + " " + mag.ToString());           
        }

        void acc_ThresholdExceeded(GTM.Seeed.Accelerometer sender)
        {
            Debug.Print("Threshold exceeded.");
        }
    }
}
